package com.stox.client.ui.javafx.workbench.lifecycle;

import java.io.File;
import java.io.FilenameFilter;
import java.net.URL;
import java.net.URLClassLoader;

import javax.swing.JOptionPane;

import com.stox.shared.core.util.PathUtil;

public class StoxJavaFxClassLoader extends URLClassLoader {

	public StoxJavaFxClassLoader(ClassLoader parent) {
		super(new URL[0], parent);

		try {
			File library = new File(PathUtil.getRootPath() + "library");
			if (library.exists()) {
				File[] jars = library.listFiles(new FilenameFilter() {
					@Override
					public boolean accept(File dir, String name) {
						return name.endsWith("jar");
					}
				});
				if (null != jars && 0 != jars.length) {
					for (File jar : jars) {
						super.addURL(jar.toURI().toURL());
					}
				}
			}
			File javafxJar = new File("jar:\\"+ System.getProperty("java.home") + "\\lib\\jfxrt.jar");
			if(javafxJar.exists()) {
				super.addURL(javafxJar.toURI().toURL());
			}else{
				try{
					Class.forName("javafx.stage.Stage");
				}catch(ClassNotFoundException  | NoClassDefFoundError e1){
					JOptionPane.showMessageDialog(null,"JavaFX library not found.\nPlease install java version 1.7 or higher.\nExiting application...", "Error", JOptionPane.ERROR_MESSAGE);
					System.exit(0);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
